﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;


namespace WebAutoUtils
{
    public interface ITestDataExceptionRules
    {
        List<TestDataExceptionRules.DataExceptionRules> ListOfExceptionRules { get; set; }
        bool IsDataConnectionAllowed(string testDataConnectionString);
        bool IsStartURLAllowed(string testStartURL);
        bool IsEndOrFailURLAllowed(string testEndOrFailURL);
        bool IsExcelFileAllowed(string excelFile);
    }
    public class General
    {

        public static void SendEmail(string senderProcess,string subject,string testName,string testStatus,int iterationCount,string lineMessage,string browser,ref string errorMessage)
        {
            bool canSendEmail = ConfigurationManager.AppSettings["CanSendEmail"].ToLower() == "true" ||
                                ConfigurationManager.AppSettings["CanSendEmail"].ToLower() == "1";
            if (canSendEmail)
            {
                string smtpServer = ConfigurationManager.AppSettings["SMTPServer"];
                string fromEmail = ConfigurationManager.AppSettings["FromEmail"];
                string toEmail = ConfigurationManager.AppSettings["ToEmail"];
                string emailBody = CreatEmailHTMLBody(testName, testStatus, iterationCount, lineMessage, browser);
                string subj = subject + " - " + senderProcess + " (" + System.Environment.MachineName + ")";
                Utils.SendEmail(smtpServer, toEmail, fromEmail, subj, emailBody, ref errorMessage);
                //Thread t = new Thread(() => Utils.SendEmail(smtpServer, toEmail, fromEmail, subj, emailBody));
                //t.Start();
            }
        }


        public static string CreatEmailHTMLBody(string testName, string testStatus, int iterationCount, string lineMessage,string browser)
        {
            string returnValue = "<!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
            returnValue += "<html>";
            returnValue += "<head>";
            returnValue += "<title></title>";
            returnValue += "<style>";
            returnValue += ".tdHeader{font-family:Tahoma;font-weight:bold;font-size:9pt;}";
            returnValue += ".tdRow{font-family:Tahoma;font-size:9pt;}";
            returnValue += "</style>";
            returnValue += "</head>";
            returnValue += "<body>";
            returnValue += "<table>";
            returnValue += "<tr>";
            returnValue += "<td class='tdRow'>Dear user</td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdRow'>Date : " + DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") + "</td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'>A test has been executed by the BunnyChow-Integration Tester  v" + Utils.GetVersionNumber() + "</td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'>The test was run on the  <i style='color:blue'>" + browser + "</i> browser.</td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdRow'>Iteration Number : " + iterationCount + "</td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            string msg = "Execution of TEST(" + testName + ") status : "+testStatus+"</span>";
            if (testStatus.ToLower()=="success")
            {
                msg = "Execution of TEST(" + testName + ") was <span style='color:green'>Successful</span>";
            }
            if (testStatus.ToLower() == "fail" || testStatus.ToLower() == "failed")
            {
                msg = "Execution of TEST(" + testName + ") has <span style='color:red'>Failed</span>";
            }
            if (testStatus.ToLower() == "error")
            {
                msg = "Execution of TEST(" + testName + ") has an <span style='color:orange'>Error</span>";
            }
            returnValue += "<td class='tdRow'>" + msg + "</td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdRow'>" + lineMessage + "</td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdRow'>Thank you</td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'>BunnyChow-Integration Tester Team</td>";
            returnValue += "</tr>";
            returnValue += "</table>";
            returnValue += "</body>";
            returnValue += "</html>";
            return returnValue;
        }


        public static void SendEmail(string senderProcess, string subject, List<DataObjects.TestDataResults> listOfTestDetails , ref string errorMessage)
        {
            bool canSendEmail = ConfigurationManager.AppSettings["CanSendEmail"].ToLower() == "true" ||
                                ConfigurationManager.AppSettings["CanSendEmail"].ToLower() == "1";
            if (canSendEmail)
            {
                string smtpServer = ConfigurationManager.AppSettings["SMTPServer"];
                string fromEmail = ConfigurationManager.AppSettings["FromEmail"];
                string toEmail = ConfigurationManager.AppSettings["ToEmail"];
                string emailBody = CreatEmailHTMLBody(listOfTestDetails);
                string subj = subject + " - " + senderProcess + " (" + System.Environment.MachineName + "-" + Environment.UserName + ")";
                Utils.SendEmail(smtpServer, toEmail, fromEmail, subj, emailBody, ref errorMessage);
                //Thread t = new Thread(() => Utils.SendEmail(smtpServer, toEmail, fromEmail, subj, emailBody));
                //t.Start();
            }
        }

        public static string CreatEmailHTMLBody(List<DataObjects.TestDataResults> listOfTestDetails)
        {
            string returnValue = "<!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
            returnValue += "<html>";
            returnValue += "<head>";
            returnValue += "<title></title>";
            returnValue += "<style>";
            returnValue += ".tdHeader{font-family:Tahoma;font-weight:bold;font-size:9pt;}";
            returnValue += ".tdRow{font-family:Tahoma;font-size:9pt;}";
            returnValue += "</style>";
            returnValue += "</head>";
            returnValue += "<body>";
            returnValue += "<table>";
            returnValue += "<tr>";
            returnValue += "<td class='tdRow'>Dear user</td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdRow'>Date : " + DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") + "</td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'>The following test(s) have been executed by the BunnyChow-Integration Tester  v" + Utils.GetVersionNumber() + "</td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'>";
            returnValue += "<table style='width:98%;'>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'><u>Browser</u></td>";
            returnValue += "<td class='tdHeader'><u>Test</u></td>";
            returnValue += "<td class='tdHeader'><u>Status</u></td>";
            returnValue += "<td class='tdHeader'><u>Message</u></td>";
            returnValue += "</tr>";
            
            foreach (var item in listOfTestDetails)
            {
                string lineMessage = "No errors";
                returnValue += "<tr>";
                returnValue += "<td class='tdRow'>" + item.TestBrowser + "</td>";
                returnValue += "<td class='tdRow'>" + item.TestName + "</td>";
                if(!item.IsFailed && !item.IsFrameworkErr)
                {
                    returnValue += "<td class='tdRow'><span style='color:green'>Success</span></td>";
                }
                if (item.IsFailed && item.IsFrameworkErr)
                {
                    returnValue += "<td class='tdRow'><span style='color:orange'>Error</span></td>";
                    lineMessage = item.FrameworkErrorMessage;
                }

                if (item.IsFailed && !item.IsFrameworkErr)
                {
                    returnValue += "<td class='tdRow'><span style='color:orange'>Failed</span></td>";
                    lineMessage = item.FailErrorMessage;
                }

                returnValue += "<td class='tdRow'>" + lineMessage + "</td>";
               
                returnValue += "</tr>";
               


            }
            returnValue += "</table>";
            returnValue += "</td>";
            returnValue += "</tr>";
           
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'></td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdRow'>Thank you</td>";
            returnValue += "</tr>";
            returnValue += "<tr>";
            returnValue += "<td class='tdHeader'>BunnyChow-Integration Tester Team</td>";
            returnValue += "</tr>";
            returnValue += "</table>";
            returnValue += "</body>";
            returnValue += "</html>";
            return returnValue;
        }

        public static ITestDataExceptionRules GetExceptionRules()
        {
            ITestDataExceptionRules iTestDataExceptionRules = new TestDataExceptionRules();
            var listofExceptions = new List<TestDataExceptionRules.DataExceptionRules>();
            var exceptions = new TestDataExceptionRules.DataExceptionRules();
            exceptions.RuleName = TestDataExceptionRules.ExceptionRuleName.DatabaseConnectionString;
            exceptions.NotAllowedValue = ConfigurationManager.AppSettings["DbConnectionNotAllowed"];
            listofExceptions.Add(exceptions);

            exceptions.RuleName = TestDataExceptionRules.ExceptionRuleName.TestStartURL;
            exceptions.NotAllowedValue = ConfigurationManager.AppSettings["TestStartURLNotAllowed"];
            listofExceptions.Add(exceptions);

            exceptions.RuleName = TestDataExceptionRules.ExceptionRuleName.TestEndOrFailURL;
            exceptions.NotAllowedValue = ConfigurationManager.AppSettings["TestEndOrFailURLNotAllowed"];
            listofExceptions.Add(exceptions);

            exceptions.RuleName = TestDataExceptionRules.ExceptionRuleName.TestExcelFile;
            exceptions.NotAllowedValue = ConfigurationManager.AppSettings["TestExcelFileNotAllowed"];
            listofExceptions.Add(exceptions);

            iTestDataExceptionRules.ListOfExceptionRules = listofExceptions;

            return iTestDataExceptionRules;
        }

    }
}
